package com.virus.server.classloader;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class AppBaseClassLoader extends URLClassLoader {
    public AppBaseClassLoader(String docBase, ClassLoader parent) {
        super(new URL[]{}, parent);
        File webInfo = new File(docBase, "WEB-INF");
        if (!webInfo.exists()) {
            return;
        }
        File classes = new File(webInfo, "classes");
        File lib = new File(webInfo, "lib");
        if (lib.exists()){
            File[] jars = lib.listFiles();
            for (File jar : jars) {
                if (jar.getName().endsWith(".jar")) {
                    try {
                        addURL(new URL("file:" + jar.getAbsolutePath()));
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        if (classes.exists()) {
            try {
                addURL(new URL("file:" + classes.getAbsolutePath() + "/"));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }
}
